package com.qianfeng.biquge.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.biquge.constans.ResultCode;
import com.qianfeng.biquge.exceptions.AddDataException;
import com.qianfeng.biquge.exceptions.QueryDataException;
import com.qianfeng.biquge.exceptions.UpdateDataException;
import com.qianfeng.biquge.mapper.AuthorMapper;
import com.qianfeng.biquge.pojo.Authors;
import com.qianfeng.biquge.service.IAuthorService;
import com.qianfeng.biquge.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;


@Service
@Transactional
public class AuthorServiceImpl implements IAuthorService {

    @Autowired
    private AuthorMapper authorMapper;


    @Override
    public void addAuthor(Authors authors) {

        if (authors==null || authors.getAuthorname()==null){

            throw new AddDataException("添加作者失败,作者姓名不能为空", ResultCode.AUTHOR_NAME_NULL);

        }

        authorMapper.addAuthor(authors);


    }

    @Override
    public PageInfo<Authors> getAllAuthor(int pageNum, int pageSize) {



        PageHelper.startPage(pageNum, pageSize);

        List<Authors> authorsList = authorMapper.getAllAuthor();

        if (authorsList==null){

            throw new QueryDataException("查询作者失败", ResultCode.AUTHOR_NULL);

        }

        PageInfo<Authors> pageInfo=new PageInfo<Authors>(authorsList);

        return pageInfo;


    }

    @Override
    public Authors getAuthorByAuthorId(Long authorId) {

        if (authorId==null || authorId <= 0){

            throw new QueryDataException("查询作者失败,ID不能为空", ResultCode.NULL_DATA);

        }

        Authors authors = authorMapper.getAuthorByAuthorId(authorId);

        if (authors==null){

            throw new QueryDataException("查询作者失败,ID不存在",ResultCode.AUTHOR_NULL);

        }

        return authors;


    }

    @Override
    public PageInfo<Authors> getAuthorByAuthorName(String authorName, int pageNum, int pageSize) throws UnsupportedEncodingException {

        if (authorName==null){

            throw new QueryDataException("查询作者失败,名字不能为空",ResultCode.AUTHOR_NAME_NULL);

        }

        String decode = URLDecoder.decode(authorName, "UTF-8");


        PageHelper.startPage(pageNum, pageSize);

        List<Authors> authorsList = authorMapper.getAuthorByAuthorName(decode);

        if (authorsList==null){

            throw new QueryDataException("查询作者失败",ResultCode.DATA_NOT_EXIST);

        }

        PageInfo<Authors> pageInfo=new PageInfo<Authors>(authorsList);

        return pageInfo;


    }

    @Override
    public int updateAuthor(Authors authors) {

        if (authors==null || authors.getAuthorid()==null || authors.getAuthorname()==null){

            throw new UpdateDataException("修改作者失败,ID/名字不能为空",ResultCode.AUTHOR_NAME_NULL);

        }

        int i = authorMapper.updateAuthor(authors);

        if (i<=0){

            throw new UpdateDataException("更新数据失败,ID不存在", ResultCode.DATA_NOT_EXIST);

        }

        return i;


    }
}
